package org.example.controller;

import org.example.annotation.CheckPermission;
import org.example.ourEnum.AnnouncementStatus;
import org.example.pojo.domain.Announcement;
import org.example.result.Result;
import org.example.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/announcements")
@CrossOrigin
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    // 馆长创建公告的草稿
    // 博物馆管理人员可以馆长创建公告的草稿
    @CheckPermission({"curator"})
    @PostMapping("/createDraft")
    public Result createDraft(@RequestBody Announcement announcement) {
        announcementService.createDraft(announcement);
        return Result.success("创建公布草稿成功");
    }

    // 通过id查看一个公告详细信息
    // 所有人都可以
    @CheckPermission({"visitor", "curator", "admin"})
    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") Integer id) {
        return Result.success("通过id获得announcement成功", announcementService.getById(id));
    }

    // 查看所有公告
    // 所有人都可以
    @CheckPermission({"visitor", "curator", "admin"})
    @GetMapping("/list")
    public Result getAll() {
        return Result.success("查看所有公告成功", announcementService.getAll());
    }

    // 更新公告
    // 只有馆长可以更新公告
    @CheckPermission({"curator"})
    @PutMapping("/update")
    public Result update(@RequestBody Announcement announcement) {
        announcementService.update(announcement);
        return Result.success("更新成功");
    }

    // 通过id删除公告
    // 只有馆长可以通过id删除公告
    @CheckPermission({"curator"})
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        announcementService.delete(id);
        return Result.success("删除成功");
    }

    // 更改公告状态，只用于将公告状态从 草稿 更改为 发布 状态
    // 只有馆长可以更改公告状态
    @CheckPermission({"curator"})
    @PostMapping("/changeStatus")
    public Result changeStatus(@RequestBody Announcement announcement) {
        announcementService.changeStatus(announcement.getAnnouncementId(), AnnouncementStatus.published);
        return Result.success("改变状态成功");
    }
}
